/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nhadat.action;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import nhadat.action.form.InsertTinDangActionForm;
import nhadat.action.form.LoginActionForm;
import nhadat.xml.DatabaseAndXMLFileMappingSyncrolization;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author huanht
 */
public class InsertTinDangAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        request.setCharacterEncoding("UTF-8");

        String action = request.getParameter("action");
        if ("DirectSubmit".equals(action)) {

            InsertTinDangActionForm f = (InsertTinDangActionForm) form;
            LoginActionForm loginForm = (LoginActionForm) request.getSession().getAttribute("LoginActionForm");
            String username = loginForm.getTendangnhap();
            FormFile myFile = f.getTheFile();
            String fileName = myFile.getFileName();
            String filePath = getServlet().getServletContext().getRealPath("/") + "images\\";

            if (!fileName.equals("")) {
                System.out.println("Server path:" + filePath);
                File fileToCreate = new File(filePath, fileName);
                if (!fileToCreate.exists()) {
                    FileOutputStream fileOutStream = new FileOutputStream(fileToCreate);
                    fileOutStream.write(myFile.getFileData());
                    fileOutStream.flush();
                    fileOutStream.close();
                }
            }
            if (!fileName.equals("")) {
                f.InSertToXMLFull("images/" + fileName, username);
            } else {
                f.InSertToXMLFull(" ", username);
            }
            if (!fileName.equals("")) {
                f.InsertToXML("images/" + fileName, username);
            } else {
                f.InsertToXML(" ", username);
            }

//        String path = getServlet().getServletContext().getRealPath("/");
//        String tindangFullXML = path + "xml/TindangFull.xml";
//        String loainhaFullXML = path + "xml/TblLoaiNha.xml";
//        String loaigiaytoFullXML = path + "xml/TblLoaiGiayTo.xml";
//        String khuvucFullXML = path + "xml/TblKhuVuc.xml";
//        String loaitinFullXML = path + "xml/TblLoaiTin.xml";
//
//        DatabaseAndXMLFileMappingSyncrolization syn = new DatabaseAndXMLFileMappingSyncrolization();
//        syn.InsertUpdateDelete(tindangFullXML, loainhaFullXML, loaigiaytoFullXML,
//                khuvucFullXML, loaitinFullXML);

            f.recordForReport(username);
        } else if ("SubmitAll".equals(action)) {
            InsertTinDangActionForm f = (InsertTinDangActionForm) form;
            LoginActionForm loginForm = (LoginActionForm) request.getSession().getAttribute("LoginActionForm");
            String username = loginForm.getTendangnhap();

            Document doc = f.stringToDOM(f.getDsTinDangSave());

            insertAllTinMoi(doc, username, f);

        }
        return mapping.findForward(SUCCESS);
    }

    private void insertAllTinMoi(Node node, String username, InsertTinDangActionForm f) {
        if (node == null) {
            return;
        }

        if (node.getNodeName().equals("tenloainha")) {
            f.setLoainha(node.getTextContent());
        }
        if (node.getNodeName().equals("tenloaigiayto")) {
            f.setTenloaigiayto(node.getTextContent());
        }
        if (node.getNodeName().equals("cautruc")) {
            f.setCautruc(node.getTextContent());
        }
        if (node.getNodeName().equals("thongtinchitiet")) {
            f.setThongtinchitiet(node.getTextContent());
        }
        if (node.getNodeName().equals("dientichkhuonvien")) {
            f.setDvddientichkhuonvien(node.getChildNodes().item(0).getTextContent());
            f.setSolieudientichkhuonvien(node.getChildNodes().item(1).getTextContent());
        }
        if (node.getNodeName().equals("chieungangkhuonvien")) {
            f.setDvdchieungangkhuonvien(node.getChildNodes().item(0).getTextContent());
            f.setSolieuchieungangkhuonvien(node.getChildNodes().item(1).getTextContent());
        }
        if (node.getNodeName().equals("chieudaikhuonvien")) {
            f.setDvdchieudaikhuonvien(node.getChildNodes().item(0).getTextContent());
            f.setSolieuchieudaikhuonvien(node.getChildNodes().item(1).getTextContent());
        }
        if (node.getNodeName().equals("dientichxaydung")) {
            f.setDvddientichxaydung(node.getChildNodes().item(0).getTextContent());
            f.setSolieudientichxaydung(node.getChildNodes().item(1).getTextContent());
        }
        if (node.getNodeName().equals("chieungangxaydung")) {
            f.setDvdchieungangxaydung(node.getChildNodes().item(0).getTextContent());
            f.setSolieuchieungangxaydung(node.getChildNodes().item(1).getTextContent());
        }
        if (node.getNodeName().equals("chieudaixaydung")) {
            f.setDvdchieudaixaydung(node.getChildNodes().item(0).getTextContent());
            f.setSolieuchieudaixaydung(node.getChildNodes().item(1).getTextContent());
        }
        if (node.getNodeName().equals("diachi")) {
            f.setDiachi(node.getTextContent());
        }
        if (node.getNodeName().equals("khuvuc")) {
            f.setQuanhuyen(node.getChildNodes().item(0).getTextContent());
            f.setTinhthanh(node.getChildNodes().item(1).getTextContent());
        }
        if (node.getNodeName().equals("giatien")) {
            f.setDvdgiatien(node.getChildNodes().item(0).getTextContent());
            f.setSolieugiatien(node.getChildNodes().item(1).getTextContent());
        }
        if (node.getNodeName().equals("hinhanh")) {
            //f.setTheFile(node.getTextContent());
        }
        if (node.getNodeName().equals("tieudetin")) {
            f.setTieude(node.getTextContent());
        }
        if (node.getNodeName().equals("tenloaitin")) {
            f.setLoaitin(node.getTextContent());

            //FormFile myFile = f.getTheFile();
            //String fileName = myFile.getFileName();
            String fileName = "";
            String filePath = getServlet().getServletContext().getRealPath("/") + "images\\";

//            if (!fileName.equals("")) {
//                System.out.println("Server path:" + filePath);
//                File fileToCreate = new File(filePath, fileName);
//                if (!fileToCreate.exists()) {
//                    FileOutputStream fileOutStream = new FileOutputStream(fileToCreate);
//                    fileOutStream.write(myFile.getFileData());
//                    fileOutStream.flush();
//                    fileOutStream.close();
//                }
//            }
            if (!fileName.equals("")) {
                f.InSertToXMLFull("images/" + fileName, username);
            } else {
                f.InSertToXMLFull(" ", username);
            }
            if (!fileName.equals("")) {
                f.InsertToXML("images/" + fileName, username);
            } else {
                f.InsertToXML(" ", username);
            }
            
            f.recordForReport(username);
        }

        NodeList children = node.getChildNodes();
        int i = 0;
        while (i < children.getLength()) {
            insertAllTinMoi(children.item(i++), username, f);
        }
    }
}
